//ExamResultPage

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:learning/components/MyText.dart';
import 'package:learning/generated/l10n.dart';
import 'package:syncfusion_flutter_gauges/gauges.dart';

class ExamResultPage extends StatefulWidget {
  final Object? arguments;

  /// create BrnBubbleText
  const ExamResultPage({super.key, this.arguments});

  @override
  // ignore: library_private_types_in_public_api
  _ExamResultPageState createState() => _ExamResultPageState();
}

class _ExamResultPageState extends State<ExamResultPage> {
  late Map<String, dynamic> param;
  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    SystemChrome.setSystemUIOverlayStyle(
        SystemUiOverlayStyle(statusBarColor: Colors.black));
    param = widget.arguments as Map<String, dynamic>;
  }

  @override
  Widget build(BuildContext context) {
    print(param);
    double score = (param.containsKey("integral") ? param['integral'] : 0.0);
    if (score == 0.0) {
      int sd = (param.containsKey("qualified") ? param['qualified'] : 0.0);
      score = sd.toDouble();
    }
    return Scaffold(
        appBar: AppBar(
          centerTitle: true,
          //  title: MyText(S.of(context).monikaoshi),
          foregroundColor: Colors.black,
          backgroundColor: Colors.white,
          automaticallyImplyLeading: true,
        ),
        body: Container(
          //  color: Colors.black12,

          child: Column(
              // 主轴对齐方式，默认为 CrossAxisAlignment.start
              crossAxisAlignment: CrossAxisAlignment.start,
              // 垂直方向间距
              mainAxisAlignment: MainAxisAlignment.start,
              mainAxisSize: MainAxisSize.max,
              children: [
                Container(
                    color: Colors.white,
                    height: 200,
                    child: Center(
                      child: _getRadialGauge(score),
                    )),
                Container(child: _getLietView()),
                Container(
                    padding: EdgeInsets.all(10),
                    width: MediaQuery.of(context).size.width,
                    child: InkWell(
                        onTap: () {
                          Navigator.pushNamedAndRemoveUntil(context, "mainexam",
                              (Route<dynamic> route) => false);
                          //  Navigator.pushReplacement(context, newRoute)
                          //   Navigator.restorablePopAndPushNamed(context,"mainexam");
                        },
                        child: Container(
                          padding: EdgeInsets.all(10),
                          decoration: BoxDecoration(
                            color: Colors.blue,
                            borderRadius: BorderRadius.all(Radius.circular(10)),
                          ),
                          child: Center(
                              child: MyText(S.current.home,
                                  style: TextStyle(color: Colors.white))),
                        ))),
              ]),
        ));
  }

  Widget _getLietView() {
    List<Widget> list = [];
    list.add(_ListGetItem(S.current.kaoshitotal,
        param.containsKey("count") ? param['count'] : 0));
    list.add(_ListGetItem(S.current.kaoshicorrect,
        param.containsKey("qualified") ? param['qualified'] : 0));
    list.add(_ListGetItem(S.current.kaoshierrors,
        param.containsKey("failed") ? param['failed'] : 0));
    list.add(_ListGetItem(S.current.kaoshiscore,
        param.containsKey("integral") ? param['integral'] : 0));
/*
    data.putIfAbsent("faileds", () => this.faileds);
    data.putIfAbsent("qualifieds", () => this.qualifieds);
    data.putIfAbsent("count", () => this.count);
    data.putIfAbsent("success", () => true);
    data.putIfAbsent("subject", () => this.subject);
    data.putIfAbsent("integral", () => this.integral);
    data.putIfAbsent("failed", () => this.failed);
    data.putIfAbsent("qualified", () => this.qualified);*/

    return Column(children: list);
  }

  Widget _ListGetItem(String title, dynamic value) {
    if (value == null) {
      value = "0";
    }
    return ListTile(
      shape: BeveledRectangleBorder(
        side: BorderSide(color: Colors.black12, width: 0.2),
      ),
      title: MyText(title),
      trailing: MyText(
        "$value",
        style: TextStyle(fontSize: 18),
      ),
      selectedTileColor: Colors.blue,
    );
  }

  Widget _getRadialGauge(double score) {
    int sds = 0;
    if (score != null) {
      sds = score.toInt();
    }
    return SfRadialGauge(axes: <RadialAxis>[
      RadialAxis(
          minimum: 0,
          maximum: 100,
          useRangeColorForAxis: true,
          showTicks: false,
          showLabels: false,
          pointers: <GaugePointer>[
            RangePointer(value: score, dashArray: <double>[2, 5]),
            RangePointer(
              value: score,
              width: 5,
              pointerOffset: 15,
              color: Colors.blue,
            )
          ],
          annotations: <GaugeAnnotation>[
            GaugeAnnotation(
                widget: Container(
                    child: Text('$sds',
                        style: TextStyle(
                            fontSize: 35,
                            fontWeight: FontWeight.bold,
                            color: Colors.black))),
                angle: 70,
                positionFactor: 0.1)
          ],
          axisLineStyle:
              AxisLineStyle(dashArray: <double>[2, 5], color: Colors.black12))
    ]);
  }
}
